# Code for Table H1: Effect of Pretrial Incarceration on 2012 Turnout by Race with Instrument Constructed Separately by Race

rm(list = ls())

## ---------------------------------------
## Load Packages 
## ---------------------------------------
require('AER')
require('ivpack')
require('dplyr')
require('data.table')

## ---------------------------------------
## Load Data and Functions
## ---------------------------------------
load("../data0812.RData")

directory <- "../functions/"
functions <- list.files(directory)  
loadfunctions <- sapply(functions, FUN = function(x)source(paste0(directory, x)))

## ---------------------------------------
# Subset by race
## ---------------------------------------
data0812.black <- data0812[race == "Black"]
data0812.white <- data0812[race == "White"]

## ---------------------------------------
# Construct Instrument
## ---------------------------------------
data0812.black <- constructIV(data0812.black)
data0812.white <- constructIV(data0812.white)

## ---------------------------------------
#  Select Last Case Before Election
## ---------------------------------------
data0812.black <- lastCase(data0812.black)
data0812.white <- lastCase(data0812.white)

## ---------------------------------------
## Two Stage Least Squares Specifications
## ---------------------------------------
time.controls <- "as.factor(court_time1) + as.factor(court_time2) + as.factor(court_dow) + as.factor(court_shift) + as.factor(totOGS2)"
case.controls <-   "as.factor(any_drug_2) +  as.factor(any_violent_2) + as.factor(fire_arms_2) +  as.factor(any_rob_2) + as.factor(any_dui_2) + as.factor(prior_offender_2)"
demo.controls <- "age_2012 + I(age_2012^2) + Female + vote2008 + as.factor(noteli08) + regis_before"

outc.1 <- "vote2012"
endo.1 <- "pti"
inst.1 <- "judgeiv"

form.1 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "|", inst.1, "+", time.controls))
form.2 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "|", inst.1, "+", time.controls, "+", demo.controls))
form.3 <- formula(paste(outc.1, "~", endo.1, "+" , time.controls, "+", demo.controls, "+", case.controls, "|", inst.1, "+", time.controls, "+", demo.controls, "+", case.controls))

## ---------------------------------------
## Row 1: Black Defendants
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812.black)
m1a2 <- ivreg(form.2, data = data0812.black)
m1a3 <- ivreg(form.3, data = data0812.black)

m1a1_d <- summary(m1a1, diagnostic = T)$diagnostics[1, 3]
m1a2_d <- summary(m1a2, diagnostic = T)$diagnostics[1, 3]
m1a3_d <- summary(m1a3, diagnostic = T)$diagnostics[1, 3]

cat("\nTable H1: Effect of Pretrial Incarceration on 2012 Turnout by Race with Instrument Constructed Separately by Race\n\n")
cat("Printing Table H1, Row 1. Black\n")
print(round(c(robust.se(m1a1)[2, c(1:2,4)], robust.se(m1a2)[2, c(1:2,4)], robust.se(m1a3)[2, c(1:2,4)]),3))
print(round(c(m1a1_d,m1a2_d,m1a3_d),3))
print(dim(data0812.black)[1])


## ---------------------------------------
## Row 2: White Defendants
## ---------------------------------------

m1a1 <- ivreg(form.1, data = data0812.white)
m1a2 <- ivreg(form.2, data = data0812.white)
m1a3 <- ivreg(form.3, data = data0812.white)

m1a1_d <- summary(m1a1, diagnostic = T)$diagnostics[1, 3]
m1a2_d <- summary(m1a2, diagnostic = T)$diagnostics[1, 3]
m1a3_d <- summary(m1a3, diagnostic = T)$diagnostics[1, 3]

cat("\nPrinting Table H1, Row 2. White\n")
print(round(c(robust.se(m1a1)[2, c(1:2,4)], robust.se(m1a2)[2, c(1:2,4)], robust.se(m1a3)[2, c(1:2,4)]),3))
print(round(c(m1a1_d,m1a2_d,m1a3_d),3))
print(dim(data0812.white)[1])

